home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OCFINC.PAK / OCREG.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  11KB  |  365 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.10  $
  6. //
  7. // OLE Registration definitions
  8. //----------------------------------------------------------------------------
  9. #if !defined(OCF_OCREG_H)
  10. #define OCF_OCREG_H
  11.  
  12. #if !defined(OCF_AUTODEFS_H)
  13. # include <ocf/autodefs.h>
  14. #endif
  15. #if !defined(WINSYS_REGISTRY_H)
  16. # include <winsys/registry.h>
  17. #endif
  18. #if !defined(CLASSLIB_POINTER_H)
  19. # include <classlib/pointer.h>
  20. #endif
  21.  
  22. #if defined(BI_NAMESPACE)
  23. namespace OCF {
  24. #endif
  25.  
  26. //
  27. // Global registration functions for OLE. Most are wrappers for winsys's
  28. // TRegistry functions
  29. //
  30.  
  31. long                                   // returns numeric value of <docflags>
  32. OcRegisterClass(TRegList& regInfo,     // obj holding array of reg parameters
  33.                 HINSTANCE hInst,       // 0 defaults to current task
  34.                 ostream& out,          // open ostream to stream reg entries
  35.                 TLangId lang,          // language for registration
  36.                 char* filter = 0,      // internal use to restrict entries
  37.                 TRegItem* defaults = 0,// optional registration default list
  38.                 TRegItem* overrides= 0);// optional registration override list
  39.  
  40. int                                   // returns error count, 0 if success
  41. OcUnregisterClass(TRegList& regInfo,  // registration table used for register
  42.                   TRegItem* overrides=0); // optional additional regitration item
  43.  
  44. inline int OcRegistryValidate(istream& in) {
  45.   return TRegistry::Validate(TRegKey::ClassesRoot, in);
  46. }
  47.  
  48. inline void OcRegistryUpdate(istream& in) {
  49.   TRegistry::Update(TRegKey::ClassesRoot, in);
  50. }
  51.  
  52. int                   // return: 0=no debug reg, -1=user clsid, 1=default used
  53. OcSetupDebugReg(TRegList& regInfo,    // original registration list
  54.                 TRegItem* regDebug,   // temp override list[DebugReplaceCount]
  55.                 TLangId lang,         // language for registration
  56.                 char* clsid);         // default temp debug clsid string buf
  57.  
  58. //
  59. const int DebugRegCount = 4+1;        // replacements + room for terminator
  60.  
  61. extern char AppDebugFilter[];         // templates needed for debug app reg
  62. extern char DocDebugFilter[];         // templates needed for debug doc reg
  63. extern TRegItem OcRegNoDebug[];       // override list to shut off "debugger"
  64. extern TRegItem OcRegNotDll[];        // override to shut off EXE-only items
  65.  
  66. //----------------------------------------------------------------------------
  67. // TRegistrar class
  68. //
  69.  
  70. //
  71. // Application running mode and registration flags
  72. // these are initialized when processing command line or registration requests
  73. // the application's copy of the initial option flags is dynamically updated
  74. //
  75. enum TOcAppMode {
  76.   amRegServer    = 0x0001, // complete registration database update requested
  77.   amUnregServer  = 0x0002, // registration database unregistration requested
  78.   amAutomation   = 0x0004, // set from cmdline when EXE lauched for automation
  79.   amEmbedding    = 0x0008, // cmdline, overridden per Instance if embedded DLL
  80.   amLangId       = 0x0010, // user registration requested a particular LangId
  81.   amTypeLib      = 0x0020, // requested typelib to be generated and registered
  82.   amDebug        = 0x0040, // user requested launching for debugging
  83.   amNoRegValidate= 0x0080, // user request to suppress registry validation
  84.   amExeModule    = 0x0100, // set for EXE components, 0 if DLL inproc server
  85.   amExeMode      = 0x0200, // may be overridden per instance if running DLL
  86.   amServedApp    = 0x0400, // per instance flag, app refcnt held by container
  87.   amSingleUse    = 0x0800, // set from app reg, may be forced on per instance
  88.   amQuietReg     = 0x1000, // suppress error UI during registration processing
  89.   amRun          = 0x2000, // set in factory call to run application msg loop
  90.   amShutdown     = 0x4000, // set in factory call to shutdown/delete app
  91.   amAnyRegOption = amRegServer | amUnregServer | amTypeLib,
  92. };
  93.  
  94. typedef IUnknown*
  95. (*TComponentFactory)(IUnknown* outer, uint32 options, uint32 id = 0);
  96.  
  97. class _ICLASS TAppDescriptor;
  98.  
  99. //
  100. // class TRegistrar
  101. // ~~~~~ ~~~~~~~~~~
  102. // Application registration manager interface class
  103. //
  104. class _ICLASS TRegistrar {
  105.   public:
  106.     TRegistrar(TRegList& regInfo, TComponentFactory callback,
  107.                string& cmdLine, HINSTANCE hInst = ::_hInstance);
  108.     virtual ~TRegistrar();
  109.  
  110.     // Create/Expose/Destroy automation object proxies
  111.     //
  112.     TUnknown* CreateAutoApp(TObjectDescriptor app, uint32 options,
  113.                             IUnknown* outer=0);
  114.     void      ReleaseAutoApp(TObjectDescriptor app);
  115.     TUnknown* CreateAutoObject(TObjectDescriptor doc, TServedObject& app,
  116.                                IUnknown* outer=0);
  117.     TUnknown* CreateAutoObject(const void* obj, const typeinfo& objInfo,
  118.                                const void* app, const typeinfo& appInfo,
  119.                                IUnknown* outer=0);
  120.  
  121.     virtual void*     GetFactory(const GUID& clsid, const GUID& iid);
  122.     virtual bool      CanUnload();
  123.  
  124.     // Run (run app instance if EXE) and Shutdown
  125.     //
  126.     virtual int       Run();
  127.     virtual void      Shutdown(IUnknown* releasedObj, uint32 options);
  128.  
  129.     // Walk linked list of registrars
  130.     //
  131.     static TRegistrar* GetNext(TRegistrar* reg);
  132.  
  133.     // Registration management functions
  134.     //
  135.     void              RegisterAppClass();
  136.     void              UnregisterAppClass();
  137.  
  138.     // Command line options accessors
  139.     //
  140.     bool              IsOptionSet(uint32 option) const;
  141.     uint32            GetOptions() const;
  142.     void              SetOption(uint32 bit, bool state);
  143.     void              ProcessCmdLine(string& cmdLine);
  144.  
  145.     // Accessor to AppDescriptor object (real workhorse object!)
  146.     //
  147.     TAppDescriptor&   GetAppDescriptor() {return AppDesc;}
  148.  
  149.   protected:
  150.     TRegistrar(TAppDescriptor& appDesc);
  151.     TAppDescriptor& AppDesc;
  152.  
  153.   private:
  154.  
  155.     // Members to maintain linked list of registrar objects
  156.     //
  157.     static TRegistrar* RegistrarList;
  158.     TRegistrar*        Next;
  159. };
  160.  
  161. //----------------------------------------------------------------------------
  162. //  Factory for automated OLE components, no linking/embedding support
  163. //
  164.  
  165. //
  166. // Simple factor for COM Servers
  167. //
  168. template <class T> class TOcComFactory {
  169.   public:
  170.     operator TComponentFactory() {return Create;}
  171.  
  172.     // Main Create callback function called to create app and/or object
  173.     //
  174.     static IUnknown* Create(IUnknown* outer, uint32 options, uint32 id);
  175. };
  176.  
  177. // Main Create callback function called to create COM object
  178. //
  179. template <class T> IUnknown*
  180. TOcComFactory<T>::Create(IUnknown* outer, uint32 /*options*/, uint32 /*id*/)
  181. {
  182.   // Create instance of our object
  183.   //
  184.   T *objPtr = new T;
  185.  
  186.   // Set the controlling IUnknown
  187.   //
  188.   objPtr->SetOuter(outer);
  189.  
  190.   // Return IUnknown [operator automatically calls AddRef()]
  191.   //
  192.   return objPtr->operator IUnknown*();
  193. }
  194.  
  195. //
  196. // Simple factory for Automation Server
  197. //
  198. template <class T> class TOcAutoFactory {
  199.   public:
  200.     operator TComponentFactory() {return Create;}
  201.  
  202.     // Callouts to allow replacement of individual creation steps
  203.     //
  204.     static T*        CreateApp(HINSTANCE hInst, uint32 options);
  205.     static int       RunApp(T* app);
  206.     static void      DestroyApp(T* app);
  207.  
  208.     // Main Create callback function called to create app and/or object
  209.     //
  210.     static IUnknown* Create(IUnknown* outer, uint32 options, uint32 id);
  211. };
  212.  
  213. //
  214. // Called when the app is not found and needs to be created
  215. //
  216. template <class T> T*
  217. TOcAutoFactory<T>::CreateApp(HINSTANCE hInst, uint32 options)
  218. {
  219.   T* app = new T(hInst, options);
  220.   return app;
  221. }
  222.  
  223. //
  224. // Called to run the application message loop if an EXE, or DLL in amExeMode
  225. //
  226. template <class T> int
  227. TOcAutoFactory<T>::RunApp(T* /*app*/)
  228. {
  229.   MSG msg;
  230.   while(GetMessage(&msg, 0, 0, 0)) {
  231.     TranslateMessage(&msg);
  232.     DispatchMessage(&msg);
  233.   }
  234.   return 0;
  235. }
  236.  
  237. //
  238. // Called to destroy the application previously created
  239. //
  240. template <class T> void
  241. TOcAutoFactory<T>::DestroyApp(T* app)
  242. {
  243.   delete app;
  244. }
  245.  
  246. //
  247. // Main Create callback function called to create app
  248. //
  249. template <class T> IUnknown*
  250. TOcAutoFactory<T>::Create(IUnknown* outer, uint32 options, uint32 /*id*/)
  251. {
  252.   static T* exeApp;     // used to hold EXE object until OLE factory call
  253.   T* app;
  254.   IUnknown* ifc = 0;
  255.   if (options & amShutdown)
  256.     return (options & amServedApp) ? 0 : outer;
  257.   if ((options & amAutomation) && (options & amServedApp)) {
  258.     app = exeApp;    // if EXE, retrieve app created on initial call from main
  259.   } else {
  260.     app = CreateApp(_hInstance, options);
  261.   }
  262.   if ((options & amAutomation) && !(options & amServedApp)) {
  263.     exeApp = app;    // if EXE, hold app until factory call when outer known
  264.   } else {
  265.     ifc = *::Registrar->CreateAutoApp(TAutoObjectDelete<T>(app),options,outer);
  266.   }
  267.   if (options & amRun) {
  268.     RunApp(app);
  269.     DestroyApp(app);
  270.   } // else DLL server, ifc will be released by controller, which deletes app
  271.   return ifc;
  272. }
  273.  
  274. #if defined(BI_NAMESPACE)
  275. } // namespace OCF
  276. #endif
  277.  
  278. //----------------------------------------------------------------------------
  279.  
  280. #if !defined(OCF_APPDESC_H)
  281. # include <ocf/appdesc.h>  // private for inline implementation only
  282. #endif
  283.  
  284. //
  285. //
  286. //
  287. inline TUnknown*
  288. TRegistrar::CreateAutoApp(TObjectDescriptor app, uint32 opts, IUnknown* outer) {
  289.   return AppDesc.CreateAutoApp(app, opts, outer);
  290. }
  291.  
  292. //
  293. //
  294. //
  295. inline void
  296. TRegistrar::ReleaseAutoApp(TObjectDescriptor app) {
  297.   AppDesc.ReleaseAutoApp(app);
  298. }
  299.  
  300. //
  301. //
  302. //
  303. inline TUnknown*
  304. TRegistrar::CreateAutoObject(TObjectDescriptor doc, TServedObject& app,
  305.                              IUnknown* outer) {
  306.   return AppDesc.CreateAutoObject(doc, app, outer);
  307. }
  308.  
  309. //
  310. //
  311. //
  312. inline TUnknown*
  313. TRegistrar::CreateAutoObject(const void* obj, const typeinfo& objInfo,
  314.                              const void* app, const typeinfo& appInfo,
  315.                              IUnknown* outer) {
  316.   return AppDesc.CreateAutoObject(obj, objInfo, app, appInfo, outer);
  317. }
  318.  
  319. //
  320. //
  321. //
  322. inline void
  323. TRegistrar::RegisterAppClass() {
  324.   AppDesc.RegisterClass();
  325. }
  326.  
  327. //
  328. //
  329. //
  330. inline void
  331. TRegistrar::UnregisterAppClass()  {
  332.   AppDesc.UnregisterClass();
  333. }
  334.  
  335. //
  336. //
  337. //
  338. inline bool
  339. TRegistrar::IsOptionSet(uint32 option) const {
  340.   return AppDesc.IsOptionSet(option);
  341. }
  342.  
  343. //
  344. //
  345. //
  346. inline uint32 TRegistrar::GetOptions() const {
  347.   return AppDesc.GetOptions();
  348. }
  349.  
  350. //
  351. //
  352. //
  353. inline void TRegistrar::SetOption(uint32 bit, bool state) {
  354.   AppDesc.SetOption(bit,state);
  355. }
  356.  
  357. //
  358. //
  359. //
  360. inline void
  361. TRegistrar::ProcessCmdLine(string& cmdLine) {
  362.   AppDesc.ProcessCmdLine(cmdLine);
  363. }
  364. #endif  // OCF_OCREG_H
  365.